package com.smzd.framework.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.security.web.savedrequest.SavedRequest;

public class URIStacks implements Serializable{
    private static final long serialVersionUID = -2131754784511896443L;
    public static final String URISTACKS_SESSION_KEY = "_URISTACKS_SESSION_KEY_";
    public static final String SPRING_SECURITY_SAVED_REQUEST = "SPRING_SECURITY_SAVED_REQUEST";
    private static final URIStacks instance = new URIStacks();
    List<SavedRequest> uris = new ArrayList<>();
    int top = -1;

    public static URIStacks getInstance(){
        return instance;
    }
    private URIStacks(){}
    
    public void push(SavedRequest uri){
        top++;
        if(top>=uris.size()){
            uris.add(uri);
        }else{
            uris.set(top, uri);
        }
    }
    public SavedRequest pop(){
        if(top<0){
            return null;
        }
        return uris.get(top--);
    }
    
    public SavedRequest clear(){
        SavedRequest dsr = null;
        if(size()>0){
            dsr = uris.get(0);
        }
        top = -1;
        return dsr;
    }
    
    public int size(){
        return top+1;
    }
}
